// 定义一个模块，并导出

import { GoodsInfo, ApiRes } from "@/types/data";
import request from "@/utils/request";
import { defineStore } from "pinia";

export default defineStore('goods', {
  state(){
    return {
      info: {} as GoodsInfo
    }
  },
  actions: {
       // 用户在页面上切换选中的规格，更新价格和库存
       updatePrice(id: string) {
        const g = this.info.skus.find(it => it.id === id)
        // 表示当前用户选择的sku对应的商品信息
        if(g){
          this.info.price = g.price
          this.info.oldPrice = g.oldPrice
          this.info.inventory = g.inventory
        }
      },
    async getGoodsInfo (id: string) {
      const res = await request.get<ApiRes<GoodsInfo>>('/goods', {
        params: {
          id
        }
      })
      // console.log('商品详情', res)
      this.info = res.data.result
    }
  }
})
